package com.wmcsk.dl.dl.internal;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

import com.wmcsk.dl.dl.DLPlugin;
import com.wmcsk.dl.dl.utils.DL;


/**
 * Created by 刘红亮 on 2015/7/2 17:57
 */
public class DLActivityManager {
    private List<Activity> activityStack=new LinkedList<Activity>();
    private static DLActivityManager instance=new DLActivityManager();
    private DLActivityManager(){

    }
    public synchronized static DLActivityManager getInstance(){
        return instance;
    }
    public void addActivity(Activity pluginActivity){
        activityStack.add(pluginActivity);
        DL.e("加入了一个activity: " + "pluginActivity=" + pluginActivity);
    }
    /**
     * 结束指定的插件Activity
     */
    public void finishActivity(Activity pluginActivity){
        DL.e("pluginActivity=" + pluginActivity);
        removeActivity(pluginActivity);
        //获取真正的activity并finish
        if(pluginActivity instanceof DLPlugin){
            ((DLPlugin)pluginActivity).getThat().finish();
        }

    }
    /**
     * 获取当前代理Activity（堆栈中最后一个压入的代理activity）
     */
    public Activity currentActivity() {
        try {
            int size = activityStack.size();
            if(size>0){
                return activityStack.get(size - 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishActivity() {
        int size = activityStack.size();
        if(size>0){
            Activity pluginActivity = activityStack.get(size - 1);
            if(pluginActivity!=null){//结束并移除
                removeActivity(pluginActivity);
                //获取真正的activity并finish
                if(pluginActivity instanceof DLPlugin){
                    ((DLPlugin)pluginActivity).getThat().finish();
                }

            }
        }
    }
    /**
     * 结束指定插件类名的所有Activity
     */
    public void finishActivity(Class<?> cls) {
        for(int index=activityStack.size()-1;index>=0;index--){
            Activity pluginActivity=activityStack.get(index);
            if(pluginActivity.getClass().getName().equals(cls.getName())){
                removeActivity(pluginActivity);
                //获取真正的activity并finish
                if(pluginActivity instanceof DLPlugin){
                    ((DLPlugin)pluginActivity).getThat().finish();
                }
            }
        }
    }
    /**
     * 结束当前插件中除参数activity的所有activity
     */
    public void finishPluginOtherActivity(Activity pluginActivity){
        for(int index=activityStack.size()-1;index>=0;index--) {
            Activity mPluginActivity = activityStack.get(index);
            //包名一致，不是参数目标的都结束
            if(mPluginActivity.getPackageName().equals(pluginActivity.getPackageName())
                    &&mPluginActivity!=pluginActivity){
                removeActivity(mPluginActivity);
                //获取真正的activity并finish
                if(mPluginActivity instanceof DLPlugin){
                    ((DLPlugin)mPluginActivity).getThat().finish();
                }
            }
        }
    }
    /**
     * 移除插件中所有的activity
     * @param pluginPackageName   插件的包名，即this.getPackageName()
     */
    public void finishPluginAllActivity(String pluginPackageName){
        for(int index=activityStack.size()-1;index>=0;index--) {
            Activity mPluginActivity = activityStack.get(index);
            //包名一致，不是参数目标的都结束
            if(mPluginActivity.getPackageName().equals(pluginPackageName)){
                removeActivity(mPluginActivity);
                //获取真正的activity并finish
                if(mPluginActivity instanceof DLPlugin){
                    ((DLPlugin)mPluginActivity).getThat().finish();
                }
            }
        }
    }

    /**
     * 从列表中移除插件activity，并没有finish
     * @param pluginActivity
     */
    public void removeActivity(Activity pluginActivity){
        boolean isremove = activityStack.remove(pluginActivity);
        DL.e("DL", "removeActivity:" + pluginActivity + " --> " + isremove);
    }
    public void exitApp(){
        for(int index=activityStack.size()-1;index>=0;index--){
            Activity mPluginActivity = activityStack.get(index);
            removeActivity(mPluginActivity);
            //获取真正的activity并finish
            if(mPluginActivity instanceof DLPlugin){
                ((DLPlugin)mPluginActivity).getThat().finish();
            }
        }
    }
}
